<script lang="ts" setup>
import { onMounted, ref } from 'vue';

import { useFs } from '@fast-crud/fast-crud';

import { BaseDictItemConfig } from '#/api/basic/base/baseDictItem';
import { getValidateRulesByFs } from '#/api/common/validateByFs';
import { ActionEnum } from '#/enums/commonEnum';

import { createCrudOptions, frontRules } from './data/crud';

defineOptions({
  name: '字典管理',
  inheritAttrs: false,
});
const parentIdRef = ref();
const { crudRef, crudBinding, crudExpose, appendCrudOptions } = useFs({
  createCrudOptions,
  context: { parentIdRef },
});

// 页面打开后获取列表数据
onMounted(async () => {
  const addFormOptions = await getValidateRulesByFs({
    Api: BaseDictItemConfig.Save,
    mode: ActionEnum.ADD,
    customRules: frontRules.addForm.rules(crudExpose),
    trigger: 'change',
  });
  const editFormOptions = await getValidateRulesByFs({
    Api: BaseDictItemConfig.Update,
    mode: ActionEnum.EDIT,
    customRules: frontRules.editForm.rules(crudExpose),
    trigger: 'change',
  });
  appendCrudOptions({ ...addFormOptions, ...editFormOptions });
});

async function setData(data: any) {
  parentIdRef.value = data.parentId;
}

defineExpose({
  setData,
  doRefresh: crudExpose.doRefresh,
  setTableData: crudExpose.setTableData,
});
</script>

<template>
  <FsCrud ref="crudRef" v-bind="crudBinding" />
</template>
